//
//  WmcResolutionVC.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/11.
//

import UIKit
import YPBedrock
import RxRelay

extension WmcResolutionVC: WmcNavigatorProtocol {
    
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.resolution]
    }
    
    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcResolutionVM(aspectRatio: .ar_4_3)
        if let viewModel = parms.context as? WmcResolutionVM {
            vm = viewModel
        }
        let vc = WmcResolutionVC(vm)
        return vc
    }

}

extension WmcResolutionVC: WmcVMProtocol {
    typealias VMType = WmcResolutionVM
}


class WmcResolutionVC: WmcBaseVC {
    
    /// tableView
    private lazy var tableView: UITableView = {
        let register = YPRegister(WmcResolutionItemCelll.self)
        let tableV = UITableView.YP.Create(style: .plain, delegate: self, dataSource: self, registers: [register])
        tableV.backgroundColor = UIColor.wmc_gray_F7F8FA
        tableV.rowHeight = 70.0.wmc_autoHor
        tableV.tableFooterView = footerView
        return tableV
    }()
    
    private lazy var footerView: UIView = {
        let hMargin = 16.0.wmc_autoHor
        let vMargin = 12.0.wmc_autoHor
        let maxW = Wmc_screenWidth - hMargin * 3.0
        let view = UIView()
        tipLabel.preferredMaxLayoutWidth = maxW
        view.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(vMargin)
            make.left.equalTo(hMargin)
            make.width.equalTo(maxW)
        }
        let height = tip.yp.textHeight(maxWidth: maxW, font: tipFont) + vMargin * 2.0
        view.yp.set(height: height)
        return view
    }()
    
    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.font = tipFont
        label.textColor = UIColor.wmc_black72
        label.numberOfLines = 0
        label.text = tip
        return label
    }()
    
    private let tip = "注：选择较大的照片分辨率，可能会导致拍照或同步到云相册变慢。"
    private let tipFont = 13.wmc_scaleFont()
    /// 选中分辨率回调
    let didSelectedResolution: PublishRelay<WmcResolution> = .init()
    
    // MARK: Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.initDatasource()
    }
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        makeNavView(showShadow: false)
        title = "照片分辨率"
        view.backgroundColor = UIColor.wmc_gray_F7F8FA
        // tableView
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(8.0.wmc_autoHor)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
}

// MARK: UITableViewDataSource, UITableViewDelegate
extension WmcResolutionVC: UITableViewDataSource, UITableViewDelegate {

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcResolutionItemCelll = tableView.yp.dequeueReusableCell()
        let item = viewModel.items[indexPath.row]
        cell.item = item
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = viewModel.items[indexPath.row]
        if item.isSelected {
            didSelectedResolution.accept(item.resolution)
            navigationController?.popViewController(animated: true)
            return
        }
        viewModel.selectedItem?.isSelected = false
        item.isSelected = true
        viewModel.selectedItem = item
        tableView.reloadData()
        didSelectedResolution.accept(item.resolution)
        YPGCD.Main {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
}
